home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Assemblers / hecasm / asm3.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  5KB  |  350 lines

  1. /* this file contains the lexical functions and symbol table management */
  2. /* routines */
  3.  
  4. #include "asm.h"
  5.  
  6. /*
  7.  * Get the next non white
  8.  * character.
  9.  */
  10. getnb()
  11. {
  12. register int c;
  13.  
  14. while(((c = getraw()) == ' ') || (c == '\t'));
  15.     return(c);
  16. }
  17.  
  18.  
  19. /*
  20.  * Get next character from
  21.  * the input.
  22.  * Apply string mappings.
  23.  */
  24. getmap()
  25. {
  26. register int n, c, v;
  27.  
  28. if((c=getraw()) == '\\')
  29.     switch(c = getraw()) 
  30.         {
  31.         case 'n':
  32.             c = '\n';
  33.             break;
  34.  
  35.         case 'r':
  36.             c = '\r';
  37.             break;
  38.  
  39.         case 't':
  40.             c = '\t';
  41.             break;
  42.  
  43.         case 'b':
  44.             c = '\b';
  45.             break;
  46.  
  47.         case '0':
  48.         case '1':
  49.         case '2':
  50.         case '3':
  51.         case '4':
  52.         case '5':
  53.         case '6':
  54.         case '7':
  55.             v = n = 0;
  56.             while(++n<=3 && c>='0' && c<='7') 
  57.                 {
  58.                 v = 8*v + c - '0';
  59.                 c = getraw();
  60.                 }
  61.             putback(c);
  62.             c = v;
  63.             break;
  64.  
  65.         default:
  66.             putback(c);
  67.             c = '\\';
  68.         }
  69. return(c);
  70. }
  71.  
  72.  
  73. /*
  74.  * Get next character.
  75.  * Basic routine.
  76.  * Don't goof up on end of
  77.  * line.
  78.  */
  79. getraw()
  80. {
  81. register int c;
  82.  
  83. if((c = *sptr) != '\n')
  84.     ++sptr;
  85. else
  86.     return('\0');
  87. return(c);
  88. }
  89.  
  90.  
  91. /*
  92.  * Check for alpha.
  93.  */
  94. alpha(c)
  95. register int c;
  96. {
  97. if(c=='.' || c=='_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  98.     return(1);
  99. return(0);
  100. }
  101.  
  102.  
  103. getnum(c)
  104. int c;
  105. {
  106. char id[NCPS];
  107. int i;
  108.  
  109. i = 0;
  110. do
  111.     {
  112.     id[i++] = c;
  113.     }
  114. while(isdigit(c=getnb()));
  115. putback(c);
  116. id[i] = '\0';
  117. sscanf(id,"%d",&i);
  118. return(i);
  119. }
  120.  
  121.  
  122.  
  123. /*
  124.  * Put a character back.
  125.  * A line at a time version
  126.  * of ungetc.
  127.  * Die if too many characters
  128.  * are put back.
  129.  */
  130. putback(c)
  131. {
  132. if(c != '\0')
  133.     if(--sptr < sbuf)
  134.         exit(1);
  135. }
  136.  
  137.  
  138. /*
  139.  * Read in an indentifier.
  140.  * Store it (padded with nulls) into the
  141.  * supplied buffer.
  142.  */
  143. getid(c, id)
  144. register int c;
  145. char *id;
  146. {
  147. register char *p;
  148.  
  149. p = id;
  150. while(alpha(c) || (c>='0' && c<='9') || c == '$') 
  151.     {
  152.     if(p < &id[NCPS-1])
  153.         *p++ = c;
  154.     c = getraw();
  155.     }
  156. *p = '\0';
  157. putback(c);
  158. }
  159.  
  160.  
  161. /* convert a string to all lower case */
  162. lowerconv(str)
  163. char *str;
  164. {
  165.  
  166. while (*str != '\0')
  167.     {
  168.     if (*str >= 'A' && *str <= 'Z')
  169.         *str = *str - 'A' + 'a';
  170.     str++;
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. /* lookup a symbol in the permanent symbol table.  This is a binary search */
  177. struct sym *pstlookup(id)
  178. char *id;
  179. {
  180. int middle, left, right;
  181. int r;
  182.  
  183. left = 0;
  184. right = pstsize - 1;
  185. middle = (left + right)/2;
  186.  
  187. while((r = comp(id,pst[middle].s_name)) != 0)
  188.     {
  189.     if (r > 0)
  190.         left = middle + 1;
  191.     else
  192.         right = middle - 1;
  193.  
  194.     if (left > right)
  195.         return(NULL);
  196.     middle = (left + right)/2;
  197.     }
  198. return(&pst[middle]);
  199. }
  200.  
  201.  
  202. /* the user symbol table is a hash table therefore it must be initialized */
  203. /* so that all entries are invalid.  We must also put the location */
  204. /* counter in the table */
  205. initust()
  206. {
  207. int i;
  208. struct sym *sp;
  209.  
  210. for(i=0;i<USERMAX;i++)
  211.     ust[i].s_used = FALSE;
  212. sp = ustlookup(".");
  213. dot = sp;
  214. sp->s_used = TRUE;
  215. }
  216.  
  217.  
  218. /* user symbol table lookup.  If the item is not found create an */
  219. /* entry for it */
  220. struct sym *ustlookup(id)
  221. char *id;
  222. {
  223. register struct sym *stp;
  224. register int s, r;
  225. char *str;
  226.  
  227. /* compute the hash function */
  228. s = 0;
  229. str = id;
  230. while(*str != '\0')
  231.     s += *str++;
  232. s &= USERMAX - 1;
  233.  
  234. /* start looking for it. We use a linear collision resolution */
  235. if (ust[s].s_used)
  236.     if (!same(id,ust[s].s_name))
  237.         {
  238.     /* set it to false in case the table is full. That way we know */
  239.     /* when we wrap around */
  240.         ust[s].s_used = FALSE;
  241.         r = (s + 1) & (USERMAX - 1);
  242.         while ((ust[r].s_used) && (!same(id,ust[r].s_name))) 
  243.             r = ++r & (USERMAX - 1);
  244.  
  245.     /* wrapped all the way around */
  246.         if (r == s)
  247.             {
  248.             fprintf(stderr,"Symbol Tabel Overflow!\n");
  249.             exit(1);
  250.             }
  251.         else
  252.             ust[s].s_used = TRUE;
  253.         stp = &ust[r];
  254.         }
  255.     else
  256.         stp = &ust[s];
  257. else
  258.     stp = &ust[s];
  259.  
  260. /* if the symbol already existed */
  261. if (stp->s_used)
  262.     {
  263.     refgen(stp,lineno);
  264.     return(stp);
  265.     }
  266.  
  267. /* insert the symbol in the table */
  268. stp->s_used = TRUE;
  269.  
  270. copy(stp->s_name, id);
  271. stp->s_type = S_UND;
  272. stp->s_flag = 0;
  273. stp->s_ref = NULL;
  274. stp->s_value = 0;
  275. return(stp);
  276. }
  277.  
  278.  
  279. /*
  280.  * Record a reference for the symbol
  281.  */
  282. refgen(stp, iline)
  283. register struct sym *stp;
  284. register int iline;
  285. {
  286.  
  287. if (pass != 1) 
  288.     return;
  289.  
  290. if (refptr >= &reftab[REFMAX]) 
  291.     {
  292.     fprintf(stderr,"Reference Table Overflow!\n");
  293.     exit(1);
  294.     }
  295.  
  296. refptr->r_chain = stp->s_ref;
  297. refptr->r_stmtno = iline;
  298. stp->s_ref = refptr++;
  299. ++refcnt;
  300. return;
  301. }
  302.  
  303.  
  304. /*
  305.  * Compare two names.
  306.  * return integer >0 if a1 > a2
  307.  *                =0 if a1 = a2
  308.  *                <0 if a1 < a2
  309.  */
  310. comp(a1,a2)
  311. register char *a1, *a2;
  312. {
  313. register int c1,c2;
  314.  
  315. while((*a1 != '\0') && (*a2 != '\0'))
  316.     if((c1 = *a1++) != (c2 = *a2++)) 
  317.         return(c1 - c2);
  318. return (*a1 - *a2);
  319. }
  320.  
  321.  
  322. /* 
  323.  * compare two strings for equality 
  324.  */
  325. same(s1,s2)
  326. register char *s1, *s2;
  327. {
  328.  
  329. while(*s1 == *s2)
  330.     {
  331.     if (*s1++ == '\0')
  332.         return(TRUE);
  333.     s2++;
  334.     }
  335. return(FALSE);
  336. }
  337.  
  338.  
  339. /*
  340.  * Copy strings
  341.  */
  342. copy(s1,s2)
  343. register char *s1, *s2;
  344. {
  345.  
  346. while (*s1++ = *s2++);
  347. return;
  348. }
  349.  
  350.